//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.sys;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.SysDictCodeDao;
import com.aidong.model.wills.SysDictCode;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"sysDictCode"})
public class SysDictCodeController extends BaseController {
    @Resource
    SysDictCodeDao sysDictCodeDao;

    public SysDictCodeController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("sys/dict/list")).addObject("dictType", this.getTypeList("dictType"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "type",required = false) Integer type, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap(1);
        cd.eq("dictType", type);
        this.sysDictCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("sort"));
        ModelAndView mv = new ModelAndView("sys/dict/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam("type") String type) {
        SysDictCode dict = new SysDictCode();
        dict.setDictType(type);
        dict.setEnable(Integer.valueOf(1));
        ModelAndView mv = new ModelAndView("sys/dict/edit");
        mv.addObject("dict", dict);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("dict") SysDictCode dict) {
        if(this.chkNull(dict.getDictType())) {
            return this.getResult(false, "类别错误");
        } else if(this.chkNull(dict.getDictCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(dict.getDictName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNullZ(dict.getSort())) {
            return this.getResult(false, "请输入排序号");
        } else {
            if(dict.getEnable() == null || dict.getEnable().intValue() != 1) {
                dict.setEnable(Integer.valueOf(0));
            }

            if(this.sysDictCodeDao.chkExist(dict.getDictType(), dict.getDictCode())) {
                return this.getResult(false, "编号已存在");
            } else {
                dict.setCreateUser(this.getLoginUserCode());
                dict.setCreateTime(new Date());
                dict.setUpdateTime(new Date());

                try {
                    this.sysDictCodeDao.add(dict);
                    this.reloadDictTypeList(dict.getDictType());
                    this.addUserLog("新增其他属性", "编号:" + dict.getDictCode() + ",名称:" + dict.getDictName() + ",类型:" + this.cache.getValue("dictType", dict.getDictType()));
                } catch (Exception var3) {
                    this.log.error("add dict", var3);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("id") int id) {
        SysDictCode dict = (SysDictCode)this.sysDictCodeDao.getById(Integer.valueOf(id));
        if(dict == null) {
            return this.showAlert("没有这个记录");
        } else {
            ModelAndView mv = new ModelAndView("sys/dict/edit");
            mv.addObject("dict", dict);
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("dict") SysDictCode dict) {
        if(this.chkNullZ(dict.getAutoId())) {
            return this.getResult(false, "没有这个记录");
        } else if(this.chkNull(dict.getDictName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNullZ(dict.getSort())) {
            return this.getResult(false, "请输入排序号");
        } else {
            if(dict.getEnable() == null || dict.getEnable().intValue() != 1) {
                dict.setEnable(Integer.valueOf(0));
            }

            SysDictCode dict1 = (SysDictCode)this.sysDictCodeDao.getById(dict.getAutoId());
            if(dict1 == null) {
                return this.getResult(false, "没有这个记录");
            } else {
                dict1.setDictName(dict.getDictName());
                dict1.setDictProp(dict.getDictProp());
                dict1.setSort(dict.getSort());
                dict1.setEnable(dict.getEnable());
                dict1.setUpdateTime(new Date());

                try {
                    this.sysDictCodeDao.update(dict1);
                    this.reloadDictTypeList(dict1.getDictType());
                    this.addUserLog("修改其他属性", "编号:" + dict1.getDictCode() + ",名称:" + dict1.getDictName() + ",类型:" + this.cache.getValue("dictType", dict1.getDictType()));
                } catch (Exception var4) {
                    this.log.error("edit dict", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("id") int id) {
        SysDictCode dict = (SysDictCode)this.sysDictCodeDao.getById(Integer.valueOf(id));
        if(dict == null) {
            return this.getResult(false, "没有这个记录");
        } else {
            try {
                this.sysDictCodeDao.delete(dict);
                this.reloadDictTypeList(dict.getDictType());
                this.addUserLog("删除其他属性", "编号:" + dict.getDictCode() + ",名称:" + dict.getDictName() + ",类型:" + this.cache.getValue("dictType", dict.getDictType()));
            } catch (Exception var4) {
                this.log.error("del dict", var4);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }

    @RequestMapping({"_enable"})
    @ResponseBody
    public String _enable(@RequestParam("id") int id, @RequestParam("enable") int enable) {
        SysDictCode dict = (SysDictCode)this.sysDictCodeDao.getById(Integer.valueOf(id));
        if(dict == null) {
            return this.getResult(false, "没有这个记录");
        } else if(enable != 0 && enable != 1) {
            return this.getResult(false, "参数错误");
        } else {
            try {
                this.sysDictCodeDao.updateEnable(id, enable);
                this.reloadDictTypeList(dict.getDictType());
                this.addUserLog((enable == 1?"启用":"禁用") + "其他属性", "编号:" + dict.getDictCode() + ",名称:" + dict.getDictName() + ",类型:" + this.cache.getValue("dictType", dict.getDictType()));
            } catch (Exception var5) {
                this.log.error("edit dict enable", var5);
                return this.getResult(false, "操作发生错误");
            }

            return this.getResult(true, "操作成功");
        }
    }
}
